const { execCMD } = require("./util");
const path = require("path");
const config = require("../../config");
const spawn = require("cross-spawn");
const xcxExec = (param = []) => {
	return execCMD("cli", param);
};
//启动项目
exports.cmdOpenTool = (project_root = "") => {
	let param = ["-o"];
	project_root ? param.push(project_root) : "";
	return xcxExec(param);
};

//命令行登录
exports.cmdLogin = (option = []) => {
	let param = ["-l", "--login-qr-output", `base64@${config.loginTmp}`];
	console.log(param)
	const killSpawn = child => {
		child.kill();
	};
	return new Promise((resolve, reject) => {
		let child = spawn("cli", param, {
			encoding: "utf-8",
		});
		child.stdout.on("data", data => {
			console.log(`stdout: ${data}`);
			killSpawn(child);
			resolve(data);
		});
		child.stderr.on("data", data => {
			console.log(`stderr: ${data}`);
			killSpawn(child);
			reject(data);
		});
		child.on("error", function(code) {
			console.log(`error ${code}`);
			killSpawn(child);
			reject(code);
		});
		child.on("close", function(code) {
			console.log(`close ${code}`);
			killSpawn(child);
			reject(code);
		});
	});
	// return xcxExec(param);
};
//命令行提交预览
exports.cmdPreview = ({ project_root = "" }) => {
	let param = [
		`-p`,
		`${project_root}`,
		`--preview-qr-output`,
		`base64@${config.previewTmp}`,
	];
	console.log(param);
	return xcxExec(param);
};
//命令行上传代码
exports.cmdUpload = ({ version = "1.0.1", project_root = "", desc = "" }) => {
	let param = [
		`-u`,
		`${version}@${project_root}`,
		`--upload-desc`,
		`${desc}`,
	];
	return xcxExec(param);
};
// 命令行提交自动预览
exports.cmdAutoPreview = () => {};
//关闭开发者工具
exports.cmdClose = () => {
	let param = ["--quit"];
	return xcxExec(param);
};
